<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
class CartController extends PublicController {
	
	
    public function indexAction(){	

		$carda=session('cart');
		
/*		if(empty($carda)){
			$value = cookie('cart');
			session('cart',$value);
			$carda=session('cart');
		}*/

		$total=0;
		foreach($carda as $key=>$val){
			
			$total+=$val['subtotal'];
			
		}
        $this->assign('total',$total);
		$this->assign('carda',$carda);
		
		//print_r($carda);die;
		
		$this->display();
	
    }
	
	
	public function addcartAction(){
		
		$GAmodel=D("GoodsNAttribute"); // 实例化GoodsNAttribute对象
		
		$data=I('post.');
		
		$array=session('cart');
		
	/*	if(empty($array)){
			$value = cookie('cart');
			session('cart',$value);
			$array=session('cart');
		}*/
		

		$key=md5($data['id'].json_encode($data['attrid']));
		
		$data['number']=$data['number']+$array[$key]['number'];
		
		$data['subtotal']=$data['mdlprice']*$data['number'];
		
		$arra=array();
		foreach($data['attrid'] as $k=>$v){
			
			$arra[]=$GAmodel->where(" id=$v ")->relation('Attri')->find();
		}
		$data['attr']=$arra;
			
		$array[$key]=$data;

		session('cart',$array); //设置session
		
		//cookie('cart',$array,3600);  //设置cookie
	
		$this->success('加入购物车成功', U('cart/index'));
		
		
		}
		
		
			//ajax改变数量
	public function quantityajaxAction(){
		
		$number=I('post.number');
		$key=I('post.key');
		
		$data=session('cart');
		
/*		if(empty($data)){
			$value = cookie('cart');
			session('cart',$value);
			$data=session('cart');
		}*/
		
		$data[$key]['number']=$number;
		
		$data[$key]['subtotal']=$data[$key]['mdlprice']*$data[$key]['number'];
		
		session('cart',$data);
		//cookie('cart',$data,3600);
	}
	
	//四川某条商品
	public function delajaxAction(){
		$key=I('post.key');
		$data=session('cart');
		//$datac=cookie('cart');
		//unset($datac[$key]);
		unset($data[$key]);
		session('cart',$data);
		//cookie('cart',$datac,3600);
		
	}

	
}